// CollectionActivity.java
package com.example.hlddz2.ui.activity;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.hlddz2.R;
import java.util.ArrayList;
import java.util.List;

public class CollectionActivity extends AppCompatActivity {
    private RecyclerView rvCollection;
    private CollectionAdapter adapter;
    private List<String> collectionList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collection);

        initView();
        initData();
    }

    private void initView() {
        rvCollection = findViewById(R.id.rv_collection);
        rvCollection.setLayoutManager(new LinearLayoutManager(this));
    }

    private void initData() {
        // 模拟收藏数据
        collectionList = new ArrayList<>();
        collectionList.add("无线蓝牙耳机");
        collectionList.add("智能手表");
        collectionList.add("机械键盘");

        adapter = new CollectionAdapter(collectionList, position -> {
            // 取消收藏逻辑
            collectionList.remove(position);
            adapter.notifyDataSetChanged();
            Toast.makeText(this, "已取消收藏", Toast.LENGTH_SHORT).show();
        });
        rvCollection.setAdapter(adapter);
    }

    // 收藏适配器（内部类）
    static class CollectionAdapter extends RecyclerView.Adapter<CollectionAdapter.ViewHolder> {
        private List<String> data;
        private OnItemClickListener listener;

        public CollectionAdapter(List<String> data, OnItemClickListener listener) {
            this.data = data;
            this.listener = listener;
        }

        @Override
        public ViewHolder onCreateViewHolder(android.view.ViewGroup parent, int viewType) {
            android.view.View view = android.view.LayoutInflater.from(parent.getContext())
                    .inflate(android.R.layout.simple_list_item_1, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.tvName.setText(data.get(position));
            holder.itemView.setOnLongClickListener(v -> {
                listener.onLongClick(position);
                return true;
            });
        }

        @Override
        public int getItemCount() {
            return data.size();
        }

        static class ViewHolder extends RecyclerView.ViewHolder {
            TextView tvName;
            ViewHolder(android.view.View itemView) {
                super(itemView);
                tvName = itemView.findViewById(android.R.id.text1);
            }
        }

        interface OnItemClickListener {
            void onLongClick(int position);
        }
    }
}